package com.hwhhhh.mygrad.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.dto.UserDTO;
import com.hwhhhh.mygrad.utils.StringUtils;

public class UserTargetFragment extends BaseFragment{
    private static final int MAX_NUM = 64;
    private EditText etTarget;
    private TextView tvWord;
    private TextView tvHeader, tvSave;
    private UserDTO user;
    @Override
    protected int initLayout() {
        return R.layout.fragment_user_target;
    }

    @Override
    protected void initView() {
        etTarget = mRootView.findViewById(R.id.et_user_target);
        tvWord = mRootView.findViewById(R.id.tv_remaining_word);
        tvHeader = getActivity().findViewById(R.id.tv_header);
        tvSave = getActivity().findViewById(R.id.tv_save);
    }

    @Override
    protected void initData() {
        user = StringUtils.fromJson(getStringFromSp("user"), UserDTO.class);
        if (tvSave.getVisibility() == View.INVISIBLE) tvSave.setVisibility(View.VISIBLE);
        tvHeader.setText("修改目标");
        tvWord.setText(String.valueOf(MAX_NUM));
        etTarget.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > MAX_NUM) s.delete(MAX_NUM, s.length());    // 超出最长字符的部分减去
                int num = MAX_NUM - s.length();
                tvWord.setText(String.valueOf(num));    // 显示剩余可输入的字数
            }
        });
        etTarget.setText(user.getTarget());
    }
}
